package com.konsung.ksdc.manager.user;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.konsung.ksdc.R;
import com.konsung.ksdc.common.network.RequestCallback;
import com.konsung.ksdc.common.network.RequestUtils;
import com.konsung.ksdc.common.network.ResponseBean;
import com.konsung.ksdc.common.utils.Constants;
import com.konsung.ksdc.manager.appmain.BaseActivity;

import butterknife.Bind;
import butterknife.ButterKnife;
import okhttp3.Call;

/**
 * 修改密码页面
 * Created by Administrator on 2016/4/5 0005.
 */
public class ChangePwdActivity extends BaseActivity {

    @Bind(R.id.tab_tv_title)
    TextView tvTitle;
    @Bind(R.id.rl_back)
    RelativeLayout rlBack;
    @Bind(R.id.et_pwd)
    EditText etPwd;
    @Bind(R.id.et_pwd_confirm)
    EditText etPwdConfirm;
    @Bind(R.id.btn_confirm_change)
    Button btnConfirmChange;
    private String userPhone;

    @Override
    protected String activityDesc() {
        return getString(R.string.page_change_pwd);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_change_pwd);
        ButterKnife.bind(this);

        userPhone = getIntent().getStringExtra(RegisterCheckActivity.USER_PHONE);

        initView();
        initEvent();
    }

    /**
     * 初始化修改密码页面点击事件
     */
    private void initEvent() {
        btnConfirmChange.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                final String pwd = etPwd.getText().toString().trim();
                String pwdConfirm = etPwdConfirm.getText().toString().trim();
                if (TextUtils.isEmpty(pwd)) {
                    Toast.makeText(ChangePwdActivity.this, R.string.input_new_password, Toast
                            .LENGTH_SHORT).show();
                    return;
                }
                // 密码设置至少6位字符
                if (pwd.length() < 6) {
                    Toast.makeText(ChangePwdActivity.this, R.string.password_less_six, Toast
                            .LENGTH_SHORT).show();
                    return;
                }
                if (TextUtils.isEmpty(pwdConfirm)) {
                    Toast.makeText(ChangePwdActivity.this, R.string.new_password_confirm, Toast
                            .LENGTH_SHORT).show();
                    return;
                }
                if (!pwdConfirm.equals(pwd)) {
                    Toast.makeText(ChangePwdActivity.this, R.string
                            .tip_pwd_comfirm_error, Toast
                            .LENGTH_SHORT)
                            .show();
                    return;
                }

                if (!RequestUtils.checkNetwork(ChangePwdActivity.this)) {
                    return;
                }

                RequestUtils.updatePassword(userPhone, pwd, new RequestCallback() {
                    @Override
                    public void onError(Call call, Exception e) {
                    }

                    @Override
                    public void onResponse(ResponseBean responseBean) {
                        String resultCode = responseBean
                                .getResultCode();
                        if (resultCode.equals(Constants.REQUEST_OK)) {
                            Toast.makeText(ChangePwdActivity.this,
                                    R.string.tip_change_success,
                                    Toast.LENGTH_SHORT).show();
                            finish();
                        } else {
                            Toast.makeText(ChangePwdActivity.this,
                                    responseBean.getEntity(String.class),
                                    Toast.LENGTH_SHORT).show();
                        }
                    }
                });
            }
        });
    }

    /**
     * 初始化密码修改页面
     */
    private void initView() {
        tvTitle.setText(R.string.title_change_pwd);
        rlBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }
}
